/**
 * BanBu_CommentPhotoViewController
 * @description 本文件提供放大照片添加描述或评论的界面
 * @package
 * @author 		yinlinlin
 * @copyright
 * @version 		1.0
 * @description 本文件提供放大照片添加描述或评论的界面
 */

#import "BanBu_CommentPhotoViewController.h"
#import "NSData+Base64.h"
@interface BanBu_CommentPhotoViewController ()

@end

@implementation BanBu_CommentPhotoViewController
@synthesize photoDict = _photoDict;


- (void)dealloc
{
    self.commentCompleteCallBack = nil;
    [_photoDict release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton * _doneBu = [UIButton buttonWithType:UIButtonTypeSystem];
    [_doneBu setFrame:CGRectMake(0, 0, 47, 35)];
    [_doneBu setTitle:@"完成" forState:UIControlStateNormal];
    [_doneBu addTarget:self action:@selector(popToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * doneItem = [[UIBarButtonItem alloc]initWithCustomView:_doneBu];
    self.navigationItem.rightBarButtonItem = doneItem;
    [doneItem release];
    // Do any additional setup after loading the view from its nib.
    _commentView = [[UITextView alloc]initWithFrame:CGRectMake(10, 20, 300, 100)];
    _commentView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_commentView];
    [_commentView release];
    _commentView.returnKeyType = UIReturnKeyDone;
    _commentView.delegate = self;
    NSString *captionBase64 = [[_photoDict valueForKey:@"medialist"] valueForKey:@"text"];
    NSData *resFrom64 = [NSData dataFromBase64String:captionBase64];
    NSString *caption = [[NSString alloc] initWithData:resFrom64  encoding:NSUTF8StringEncoding];
    _commentView.text = caption;
    [caption release];
}

#pragma mark - 评论完成，返回上一界面
-(void)popToLastView
{
    if (_commentCompleteCallBack)
    {
        _commentCompleteCallBack(_commentView.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 150)
    {
        textView.text = [textView.text substringToIndex:150];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
